package stateorder

import (
	"fmt"
	"time"
)

// Order 订单
type Order struct {
	ID          string
	Items       []string
	TotalAmount float64
	State       OrderState
	CreatedAt   time.Time
	ShippedAt   time.Time
	DeliveredAt time.Time
}

// NewOrder 创建订单
func NewOrder(id string, items []string, totalAmount float64) *Order {
	order := &Order{
		ID:          id,
		Items:       items,
		TotalAmount: totalAmount,
		State:       &PendingState{},
		CreatedAt:   time.Now(),
	}
	order.TransitionTo(&PendingState{})
	return order
}

// 转换状态
func (o *Order) TransitionTo(state OrderState) {
	o.State = state
	fmt.Printf("订单 %s 状态已转换为: %s\n", o.ID, state.GetName())
}

// Pay 支付
func (o *Order) Pay() error {
	return o.State.Pay(o)
}

// Cancel 取消
func (o *Order) Cancel() error {
	return o.State.Cancel(o)
}

// Ship 发货
func (o *Order) Ship() error {
	return o.State.Ship(o)
}

// Deliver 发货
func (o *Order) Deliver() error {
	return o.State.Deliver(o)
}
